'''
Created on 05.11.2012

@author: felix_000
'''
from game import GameLogic
from PlayerColor import PlayerColor


class PentalathField(object):
    '''
    This is the class, that represents the pentalath field
    '''
    def __init__(self):
        self.width = 9
        self.height = 9
        self.topwidth = 5
        self.captured = {PlayerColor.BLACK : 0, PlayerColor.WHITE : 0}
        self.field = [[0,0,0,0,0,None,None,None,None],
                      [0,0,0,0,0,0,None,None,None],
                      [0,0,0,0,0,0,0,None,None],
                      [0,0,0,0,0,0,0,0,None],
                      [0,0,0,0,0,0,0,0,0],
                      [None,0,0,0,0,0,0,0,0],
                      [None,None,0,0,0,0,0,0,0],
                      [None,None,None,0,0,0,0,0,0],
                      [None,None,None,None,0,0,0,0,0]]
        self.neighbours = [[0,0,0,0,0,None,None,None,None],
                      [0,0,0,0,0,0,None,None,None],
                      [0,0,0,0,0,0,0,None,None],
                      [0,0,0,0,0,0,0,0,None],
                      [0,0,0,0,0,0,0,0,0],
                      [None,0,0,0,0,0,0,0,0],
                      [None,None,0,0,0,0,0,0,0],
                      [None,None,None,0,0,0,0,0,0],
                      [None,None,None,None,0,0,0,0,0]]
        self.notallowed = [[PlayerColor.NONE,PlayerColor.NONE,PlayerColor.NONE,PlayerColor.NONE,PlayerColor.NONE,None,None,None,None],
                      [PlayerColor.NONE,PlayerColor.NONE,PlayerColor.NONE,PlayerColor.NONE,PlayerColor.NONE,PlayerColor.NONE,None,None,None],
                      [PlayerColor.NONE,PlayerColor.NONE,PlayerColor.NONE,PlayerColor.NONE,PlayerColor.NONE,PlayerColor.NONE,PlayerColor.NONE,None,None],
                      [PlayerColor.NONE,PlayerColor.NONE,PlayerColor.NONE,PlayerColor.NONE,PlayerColor.NONE,PlayerColor.NONE,PlayerColor.NONE,PlayerColor.NONE,None],
                      [PlayerColor.NONE,PlayerColor.NONE,PlayerColor.NONE,PlayerColor.NONE,PlayerColor.NONE,PlayerColor.NONE,PlayerColor.NONE,PlayerColor.NONE,PlayerColor.NONE],
                      [None,PlayerColor.NONE,PlayerColor.NONE,PlayerColor.NONE,PlayerColor.NONE,PlayerColor.NONE,PlayerColor.NONE,PlayerColor.NONE,PlayerColor.NONE],
                      [None,None,PlayerColor.NONE,PlayerColor.NONE,PlayerColor.NONE,PlayerColor.NONE,PlayerColor.NONE,PlayerColor.NONE,PlayerColor.NONE],
                      [None,None,None,PlayerColor.NONE,PlayerColor.NONE,PlayerColor.NONE,PlayerColor.NONE,PlayerColor.NONE,PlayerColor.NONE],
                      [None,None,None,None,PlayerColor.NONE,PlayerColor.NONE,PlayerColor.NONE,PlayerColor.NONE,PlayerColor.NONE]]
        self.history = []
        self.notallowedHistory = []

#    def __init__(self, width, height, topwidth):
#        '''
#        Constructs the pentalath field by the given width and height.
#        '''
#        # if width / 2 is odd, constructing the matrix will be a little bit different then if width/2 is even
#        ifHalfWidthIsOdd = width % 2 == 1
#        # generate the matrix. will look like the following
#        #   0   0    None None
#        #   0   0    0    None
#        #   0   0    0    0
#        #  None 0    0    0
#        #  None None 0    0
#        # the value 0 stands for no color on this field. -1 represents, that there is no field
#        self.width = width;
#        self.height = height;
#        self.topwidth = topwidth
#        self.field = [[PlayerColor.NONE if (y < height/2 and x < topwidth + y) or (y >= height/2 and x >= y - topwidth - height/2 + ifHalfWidthIsOdd) else -1 for x in range(width)] for y in range(height)]
#        print self.field
        
    def place(self, color, x, y, isSuicide=False):
        if y < 0 or y >= self.height or x < 0 or x >= self.width:
            raise IndexError(y + ", " + x + " is an invalid index")
        ocolor = self.field[y][x]
        self.field[y][x] = color
        captured = 0
        if color == PlayerColor.NONE:
            captured = 1
            self.captured[ocolor] += 1
            self.notallowedHistory.append((x,y,self.notallowed[y][x]))
            if isSuicide:
                self.notallowed[y][x] = ocolor
            else:
                self.notallowed[y][x] = PlayerColor.NONE
        
        self.history.append((x,y, ocolor, captured))
    
    def get(self, x, y):
        return self.field[y][x]
    
    def undo(self, count):
        if count == 0:
            count = len(self.history)
        elif count > len(self.history):
            count = len(self.history)
        for i in xrange(count): #@UnusedVariable
            x, y, oldColor, captured = self.history.pop()
            self.field[y][x] = oldColor
            if captured > 0:
                self.captured[oldColor] -= captured
                x,y,color = self.notallowedHistory.pop()
                if (x,y) in self.notallowed[color]:
                    self.notallowed[y][x] = color
            
    def getAllowedFields(self, color):
        pass
    
    def __str__(self):
        ret = ""
        for y in xrange(self.height):
            ret += "\n"
            spaces = abs(y - self.height/2)
            for i in xrange(spaces):
                ret += " "
            for x in xrange(self.width):
                if self.field[y][x] is not None:
                    ret += str(self.field[y][x]) + " "
        return ret

